package com.xyulu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyulu.dao.SysUserDao;
import com.xyulu.entity.SysUserEntity;
import com.xyulu.entity.common.MessageConstant;
import com.xyulu.entity.common.Result;
import com.xyulu.entity.vo.LoginUser;
import com.xyulu.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author lumin
 * @since 2021-02-25
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Override
    public Result login(LoginUser loginUser) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginUser.getUsername(), loginUser.getPassword());
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            log.error("exception is {}",e);
            return new Result(400, MessageConstant.USER_NOT_EXIST);
        }catch (IncorrectCredentialsException e){
            log.error("exception is {}");
            return new Result(400,MessageConstant.PASSWORD_ERROR);
        }catch (AuthenticationException e) {
            log.error("exception is {}");
            return new Result(400,MessageConstant.UNKNOWN_ERROR);
        }
        return new Result(200,MessageConstant.LOGIN_SUCCESS);
    }

    @Override
    public List<String> getRoleNameByLogin(String username) {
        return baseMapper.getRoleNameByLogin(username);
    }

    @Override
    public void updaStatus(Integer status, Integer id) {
        baseMapper.updaStatus(status,id);
    }
}
